#include <stdio.h>
#define MAX 100

void print_menu()
{
	printf("0.EXIT 1.INSERT 2.MAXIMUM 3.EXTRACT_MAX\n");
	printf("please input selection:");
}

void swap(int *a,int *b)
{
	int temp = *a;
	*a = *b;
	*b = temp;
}

void keep_heap(int *arr,int n,int j)
{
	if(j * 2 + 1 >= n) 
	{
		return ;
	}
	if(arr[j] < arr[j * 2 + 1])
	{
		swap(&arr[j], &arr[j * 2 + 1]);
		keep_heap(arr, n, j * 2 + 1);
	}
	if(j * 2 + 2 >= n)
	{
		return ;
	}
	if(arr[j] < arr[j * 2 + 2])
	{
		swap(&arr[j], &arr[j * 2 + 2]);
		keep_heap(arr, n, j * 2 + 2);
	}
}

void keep_heap_up(int *arr,int n,int j)
{
	int temp;
	if(arr[j] > arr[(j - 1) / 2])
	{
		swap(&arr[j], &arr[(j - 1) / 2]);
		if((j - 1) / 2 > 0)
		{
			keep_heap_up(arr, n, (j - 1) / 2);
		}
	}
}

void heap_insert(int *arr,int *n)
{
	if(*n == MAX)
	{
		printf("Error,the queue is full.\n");
		return;
	}
	int x;
	scanf("%d",&x);
	arr[(*n)++]=x;
	keep_heap_up(arr, *n, *n - 1);
}

void heap_maximum(int *arr,int *n)
{
	if((*n) == 0)
	{
		printf("Error,the queue is empty.\n");
		return;
	}
	printf("%d\n",arr[0]);
}

void heap_exract_max(int *arr,int *n)
{
	if((*n) == 0)
	{
		printf("Error,the queue is empty.\n");
		return;
	}
	int x = 0;
	printf("%d\n",arr[0]);
	arr[0] = arr[--(*n)];
	keep_heap(arr, *n, 0);
}

int main()
{
	int selection,arr[MAX],n = 0;
	while(1)
	{
		print_menu();
		scanf("%d",&selection);
		switch(selection)
		{
			case 0:
				return 0;
			case 1:
				heap_insert(arr,&n);
				break;
			case 2:
				heap_maximum(arr,&n);
				break;
			case 3:
				heap_exract_max(arr,&n);
				break;
		}
	}
	return 0;
}
